﻿import Vue from 'vue'
import Vuex from 'vuex'
import dishStore from './modules1/dishStore'   //前台首页

// import takeStore from './modules1/takeStore'   //外卖

import canzhuoStore from "./modules1/canzhuoStore";
import paibanStore from "./modules1/paibanStore";
import lll_canzhouStore from "./modules2/lll_canzhuoStore"
import cb_caipinStore from './modules2/cb_caipinStore'
import cb_xhpStore from './modules2/cb_xhpStore'
import mxl_jbxx from './modules2/mxl_jbxx'
import mxl_hsz from './modules2/mxl_hsz'
import axios from '../http/http'
import * as api from '../http/api'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        user: {
            // 当项目加载  获取sessionStorage里面的信息  如果没有信息  初始化为undefined
            userName: sessionStorage.getItem('name') || undefined,
            userRole: sessionStorage.getItem('role') || undefined,
            token: sessionStorage.getItem('token') || undefined
        }
    },
    actions: {
        toLogin({commit}, obj) {
            return axios({
                url: api.userLogin,
                data: obj,
                method: 'post'
            }).then(res => {
                if (res.data.code == 200) {
                    sessionStorage.setItem('name', res.data.info.name)
                    sessionStorage.setItem('role', res.data.info.roleName)
                    sessionStorage.setItem('token', res.data.token)
                    commit('addUser', res)
                }
                return res.data.code
            })
        },
        loginOut({commit}) {
            sessionStorage.removeItem('name')
            sessionStorage.removeItem('token')
            sessionStorage.removeItem('role')
            commit('loginOut')
        }
    },
    mutations: {
        addUser(state, obj) {
            state.user.userName = obj.data.info.name
            state.user.userRole = obj.data.info.roleName
            state.user.token = obj.data.token
        },
        loginOut(state) {
            state.user = {
                userName: sessionStorage.getItem('name') || undefined,
                userRole: sessionStorage.getItem('role') || undefined,
                token: sessionStorage.getItem('token') || undefined
            }
        }
    },
    /*getters: {
        getType(state) {
            if (state.user.type === 'admin') {
                return '管理员'
            } else if (state.user.type === 'user') {
                return '员工'
            }
        } //getters就是vuex里面的计算属性
    },//*/
    modules: {
        dishStore,   //前台首页

        // takeStore,


        mxl_jbxx,
        mxl_hsz,
        canzhuoStore,//前台餐桌
        paibanStore,//前台排班
        lll_canzhouStore,//后台餐桌
        cb_caipinStore,//陈斌后台菜品管理
        cb_xhpStore,//陈斌后台消耗品
    }
})
